<?php
// nah ide atadjuk a legftonsoabb adatokat ugymint
//<----><------><------><------><------><------><------> seconds

// mysql params
    $username = "scuderia";
    $password = "sc|_|d3r14";
    $database = "scuderia";
    $my_host = "localhost";

function fetch_data_by_host($group, $hostname, $check, $intrval) {

global $username;
global $password;
global $database;
global $my_host;
    if (empty($group)) {
	$grp_part = "";
    } else {
	$grp_part = "and `group` = '".$group."'";
    }
    
    $to = time();
    $from = $to - $intrval;
    mysql_connect($my_host,$username,$password);
    @mysql_select_db($database) or die( "Unable to select database");
    $query  = "SELECT `value` from `checks` where time between FROM_UNIXTIME(".$from.") and FROM_UNIXTIME(".$to.") ".$grp_part." and `hostname` = '".$hostname."' and `check` = '".$check."'";
    $result = mysql_query($query);
    $retarr = array();
    while ( $row = mysql_fetch_row($result)){
	    $retarr[] = $row[0];
	}
    mysql_close();
    return $retarr;
}

function get_hosts_for_group($group) {

global $username;
global $password;
global $database;
global $my_host;

    mysql_connect($my_host,$username,$password);
    @mysql_select_db($database) or die( "Unable to select database");
    $query = "SELECT DISTINCT(hostname) from `checks` where `group` = '".$group."'";
    $result = mysql_query($query);
    $retarr = array();
    while ( $row = mysql_fetch_row($result)){
	    $retarr[] = $row[0];
	}
    mysql_close();

    return $retarr;
}

function get_time_for_host($hostname,$intrval, $check) {

global $username;
global $password;
global $database;
global $my_host;

    $to = time();
    $from = $to - $intrval;
    mysql_connect($my_host,$username,$password);
    @mysql_select_db($database) or die( "Unable to select database");
    $query  = "SELECT TIME_FORMAT(time, '%H:%i:%s') from `checks` where time between FROM_UNIXTIME(".$from.") and FROM_UNIXTIME(".$to.") and `hostname` = '".$hostname."' and `check` = '".$check."'";
    $result = mysql_query($query);
    while ( $row = mysql_fetch_array($result)){
	    $retarr[] = $row[0];
	}
    mysql_close();

    return $retarr;
}

?>
